package main

import (
	"fmt"
	"math"
)

func maxSubArray(nums []int) int {
	if len(nums) == 1 {
		return nums[0]
	}

	maxToCurrent, max := nums[0], nums[0]

	for i := 1; i < len(nums); i++ {
		maxToCurrent = int(math.Max(float64(maxToCurrent+nums[i]), float64(nums[i])))
		max = int(math.Max(float64(maxToCurrent), float64(max)))
	}

	return max
}

func main() {
	fmt.Println(maxSubArray([]int{-2, 1, -3, 4, -1, 2, 1, -5, 4}))
}
